﻿using HBase;
using hCommand;
using Net.http;
using System;
using System.Collections.Generic;

namespace Persistence {
    public   class Csd2CsEngine : GkTransaction {
        public long id;
        public long pid;
        public bool pingku=false;   
        protected override TransRes Execute() {
            CoreStockDet v = getById<CoreStockDet>(id);

            if (!BUtil.eq(v.status, StockDetailStatus.库外, StockDetailStatus.库存, StockDetailStatus.编辑)) {
                throw new Exception("明细状态异常" + v.id+"-》"+v.status);
            }
            if (BUtil.eq(v.status, StockDetailStatus.编辑)) {
                addBatch(v);
            }

            if (v.pid > 0) {
                exec("update Core_Stock_Det set pid=0,box_Code='' where id=@id", v);
               String sql= HUtil.s("select count(*) from Core_Stock_Det   where pid=@pid and status<{0}" + StockDetailStatus.上限);
                int remain = queryUnique<int>(sql, v);
                if (remain == 0) {
                    exec("update Core_Stock set classify_Id=0 where id=@pid", v);
                }
                StatUtil.stock(this, pid);
            }
            CoreStock cs = getById<CoreStock>(pid);
            if (cs!= null) {
                bulidRelation(v, cs);
                if (pingku && BUtil.eq(v.status, StockDetailStatus.库外)) {
                    String sql2 = "update Core_Stock_Det set doo_det_id=0 ,status=" + (int)StockDetailStatus.库存 + " where id=@id";
                    exec(sql2, new { id, cs.locId });

                }
            }
           
            return new TransRes();
        }
        protected  void addBatch(CoreStockDet v){
            v.status = (int)StockDetailStatus.库外;
          

            if (v.batchId == 0 && !String.IsNullOrEmpty(v.batchNo)) {
                WhBatch batch = new WhBatch();
                batch.batchNo = v.batchNo;
                batch.itemId = v.itemId;
                batch.count = v.countDb;
                WhBatch b = queryUnique<WhBatch>("select * from wh_batch where batchNo=@batchNo and item_Id=@itemId", batch);
                if (b == null) {
                    insert(batch);
                    v.batchId = batch.id;
                } else {
                    v.batchId = b.id;
                }
                exec("update Core_Stock_Det set batch_Id=@batchId, where id=@id", v);
            } else {
                exec("update Core_Stock_Det set  where id=@id", v);
            }

            StatUtil.changeBatch(this, new List<CoreStockDet>() { v });

        }
         
        protected void bulidRelation( CoreStockDet v, CoreStock cs) { 
            var item = getById<CoreItem>(v.itemId);
            if (item == null) {
                throw new Exception("物料不存在" + v.id);
            } 
            String sql1 = "SELECT DISTINCT i.classify_Id FROM `core_stock_det` d join core_item i on d.item_Id=i.id   where status<70 and pid=@id";
            List<long> ids = query<long>(sql1, cs);
            if (ids.Count > 1) {
                throw new Exception("一个托盘多个分类" + v.id + cs.classifyId);
            }else  if (ids.Count == 1) {
                if (ids[0] != item.classifyId) {
                    throw new Exception("一个托盘只能放一个分类" + v.id + ids[0]);
                }
            } else {
                String sql = "update Core_Stock set classify_Id=@classifyId  where id=@id";
                exec(sql, new { item.classifyId, id = cs.id });
            }
            String sql2 = "update Core_Stock_Det set pid=@pid,p_Code=@boxCode,p_loc=@locId  where id=@id";
            exec(sql2, new { pid = cs.id, id= v.id, cs.boxCode, cs.locId });
            StatUtil.stock(this, cs.id);
        }

      



    }
}
